package org.unigram.oluolu.recommend;

import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.unigram.oluolu.common.Query;

/**
 *
 */
public class ChangeFormatReducer extends
        Reducer<Text, Query, Text, Text> {
    
    /**
     * reduce method.
     * 
     * @param userId  user id
     * @param values  query string submitted by the user
     * @param context write key:user-id, value:set of queries
     * 
     * @throws IOException -
     * @throws InterruptedException - 
     * 
     */
    @Override
    public final void reduce(final Text userId,
            final Iterable<Query> values, final Context context)
            throws IOException, InterruptedException {

        StringBuffer featureStr = new StringBuffer();
        for (Query query : values) {
            featureStr.append(query.getQueryString());
            featureStr.append(":");
        }
        context.write(userId, new Text(featureStr.toString()));
    }
    
}
